﻿using Microsoft.Owin.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace OWINServer
{
    public class OWINStart
    {
        public static OWINTriggerEvent TriggerEvent { get; set; }

        public static OWINTriggerEvent Start(string ServerUrl)
        {
            try
            {
                // 调用Startup启动owin，url需要调用方传入
                WebApp.Start<Startup>(url: ServerUrl);

                // 新建OWINTriggerEvent类实例，并返回被调用方使用获取里面的message和infoEvent事件
                var triggerEvent = new OWINTriggerEvent();
                TriggerEvent = triggerEvent;

                HttpClient client = new HttpClient();
                // 通过get请求数据，测试owin服务是否正常开启
                var response = client.GetAsync(ServerUrl + "api/IncomingApi/testget").Result;
                if (response.IsSuccessStatusCode)
                {
                    return triggerEvent;
                }
                else
                {
                    throw new Exception("Owin loacal server start failed!");
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
